package com.duanjw.java.algorithm;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author duanjw
 */
public class TreeNode {
    public int id;
    private String name;
    private List<TreeNode> nodeList;

    public TreeNode(int id, String name, List<TreeNode> nodeList) {
        this.id = id;
        this.name = name;
        this.nodeList = nodeList;
    }

    @Override
    public String toString() {
        return "TreeNode{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", nodeList=" + nodeList +
                '}';
    }

    private String str(){
        return
                "id=" + id +
                        ", name='" + name + '\'';
    }

    public static String toString(List<TreeNode> treeNodes) {
        String result = "";
        for (TreeNode treeNode : treeNodes) {
            result += find(treeNode, "");
        }
        return result;
    }

    public static String find(TreeNode treeNode, String s) {
        s += treeNode.str();
        if(treeNode.nodeList!=null) {
            for (TreeNode node : treeNode.nodeList) {
                s = find(node, s);
            }
        }
        return s;
    }

    public static void main(String[] args) {

        TreeNode two = new TreeNode(21, "张三", null);
        TreeNode three = new TreeNode(22, "找六", Arrays.asList(two));

        List<TreeNode> root = new ArrayList<>();
        root.add(new TreeNode(1, "李四", null));
        root.add(new TreeNode(2, "王五", Arrays.asList(two, three)));

        System.out.println("分解前："+root.toString());

        System.out.println("分解后"+TreeNode.toString(root));
    }
}
